﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.ClassLibrary;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 职位管理树页面
    /// Author: tim 2010-02-23
    /// </summary>
    public partial class sys_pos_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : string.Empty;
                initializePage();
            }
        }

        private void initializePage()
        {
            createTree();
            trvPos.ExpandLevel = 1;
        }

        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvPos.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {            
            oRoot.Text = "系统职位";
            oRoot.ImageUrl = "../images/organ/pos_root.gif"; //需要替换
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0";
        }

        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            string organId = _hidOrganId.Value;
            PosBLL posBLL = new PosBLL();
            List<MsKeyValue> lstPos = posBLL.GetPosListByOrgan(organId, string.Empty);
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            foreach (MsKeyValue ePos in lstPos)
            {
                oTreeNode = createTreeNode(ePos.val, ePos.key,organId);
                oRoot.Nodes.Add(oTreeNode);
            }

            lstPos = null;
            posBLL.Release();
            posBLL = null;
        }

        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string posName, string posId, string organId)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            oNode.Text = posName;
            string sPageParam = "organId=" + organId + "&posId=" + posId;
            string encodeParam = base64Encode(sPageParam);
            oNode.NavigateUrl = "./sys_pos_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/organ/pos.gif";
            oNode.Target = "ifrmEdit";
            oNode.ID = posId;
            oNode.Type = "root";
            return oNode;
        }


        private string base64Encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }


        #region ajax methods

        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="posId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeletePos(string posId,string nodeIndex)
        {
            PosBLL posBLL = new PosBLL();
            List<string> lstResult = posBLL.Delete(posId);
            var newObj = new
            {
                OPERTYPE = "POSDELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                NODEINDEX = nodeIndex
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            posBLL.Release();
            posBLL = null;
            return serExpr;
        }
        #endregion
    }
}
